package nl.zuiderfestival.raydance.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import nl.zuiderfestival.raydance.model.net.ArduinoManager;
import nl.zuiderfestival.raydance.model.net.RadarManager;

public class ConnectionPanel extends JPanel implements Observer {

	private static final long serialVersionUID = 1L;
	
	public static final String EDITARDUINO = "EDITARDUINO";
	public static final String EDITRADAR = "EDITRADAR";
	public static final String TOGGLE_MOVEMENT_DISPLAY = "TOGGLE_MOVEMENT_DISPLAY";

	private final ImageIcon connectedIcon;
	private final ImageIcon disconnectedIcon;
	private final ImageIcon configIcon;

	private JLabel radarTouchConnectionIcon;
	private JButton radarTouchEditBtn;
	private JLabel radarInfoLabel;

	private JLabel arduinoConnectionIcon;
	private JButton arduinoEditBtn;
	private JLabel arduinoInfoLabel;

	private JCheckBox showMovementToggle;

	public ConnectionPanel() {
		super(new GridBagLayout());

		Dimension size = getPreferredSize();
		size.height = 120;
		setPreferredSize(size);
		
		connectedIcon = ImageUtil.createImageIcon(getClass(), "/images/on.png"); 
		disconnectedIcon = ImageUtil.createImageIcon(getClass(), "/images/off.png");
		configIcon = ImageUtil.createImageIcon(getClass(), "/images/edit.png");
		
		createContent();

		setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
	}
	
	public void update(Observable o, Object arg) {
		if (o instanceof RadarManager) {
			handleTUIO((RadarManager) o);
		} else if (o instanceof ArduinoManager) {
			handleArduino((ArduinoManager) o);
		}
	}

	private void handleTUIO(RadarManager manager) {
		if (manager.isConnected()) {
			radarTouchConnectionIcon.setIcon(connectedIcon);
			radarInfoLabel.setText("Listening for radartouch on port "+manager.getPort());
		} else {
			radarTouchConnectionIcon.setIcon(disconnectedIcon);
			radarInfoLabel.setText("Not listening for radartouch.");
		}
	}

	private void handleArduino(ArduinoManager manager) {
		if (manager.isConnected()) {
			arduinoConnectionIcon.setIcon(connectedIcon);
			arduinoInfoLabel.setText("Pushing arduino commands to "+manager.getHost()+":"+manager.getPort());
		} else {
			arduinoConnectionIcon.setIcon(disconnectedIcon);
			arduinoInfoLabel.setText("Not sending data to arduino.");
		}
	}

	private void createContent() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2,2,2,2);

		add(new JLabel("Radartouch"), c, 0, 0);
		radarTouchConnectionIcon = new JLabel(disconnectedIcon);
		add(radarTouchConnectionIcon, c, 1, 0);
		radarTouchEditBtn = new JButton(configIcon);
		radarTouchEditBtn.setActionCommand(EDITRADAR);
		add(radarTouchEditBtn, c, 2, 0);
		radarInfoLabel = new JLabel("Not listening for radartouch.");
		add(radarInfoLabel, c, 3, 0);
		
		add(new JLabel("Arduino"), c, 0, 1);
		arduinoConnectionIcon = new JLabel(disconnectedIcon);
		add(arduinoConnectionIcon, c, 1, 1);
		arduinoEditBtn = new JButton(configIcon);
		arduinoEditBtn.setActionCommand(EDITARDUINO);
		add(arduinoEditBtn, c, 2, 1);
		arduinoInfoLabel = new JLabel("Not sending data to arduino.");
		add(arduinoInfoLabel, c, 3, 1);
		
		showMovementToggle = new JCheckBox("Show radartouch movement");
		showMovementToggle.setActionCommand(TOGGLE_MOVEMENT_DISPLAY);
		add(showMovementToggle, c, 0, 2);
	}

	private void add(Component component, GridBagConstraints c, int gridx, int gridy) {
		c.gridx = gridx;
		c.gridy = gridy;
		add(component, c);
	}

	public void addActionListener(ActionListener l) {
		radarTouchEditBtn.addActionListener(l);
		arduinoEditBtn.addActionListener(l);
		showMovementToggle.addActionListener(l);
	}

}
